package com.yfk.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yfk.web.service.IYFKService;
import com.yfk.web.util.BIPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class YFKServiceImpl implements IYFKService {
    Logger logger = LoggerFactory.getLogger(YFKServiceImpl.class);
    @Override
    public String getAccessToken(String apiUrl, String appKey, String appSecret) throws Exception {
        return BIPUtil.getAccessToken(apiUrl, appKey, appSecret);
    }

    @Override
    public String queryYFKBillQuery(String apiUrl, Map queryParams, Object bodyParm) throws Exception {
        logger.info("-----查询友费控单据信息,请求地址:【"+apiUrl+"】,queryParam参数:【"+queryParams.toString()+"】,请求参数Body:【"+bodyParm+"】");
        JSONObject ret = new JSONObject();
        String resp = "";
        try{
            resp = BIPUtil.getPostQueryParamAndBodyParam(apiUrl, queryParams, bodyParm);
            JSONObject resjson = JSON.parseObject(resp);
            if("200".equals(resjson.getString("code"))){
                String yfkBillID = resjson.getJSONObject("data").getJSONArray("recordList").getJSONObject(0).getString("id");
                ret.put("id", yfkBillID);
            }else{
                throw new Exception(resp);
            }
        }catch(Exception e){
            logger.error("友费控 单据列表查询接口,响应信息【"+resp+"】,异常信息:【"+e.getMessage()+"】");
            throw new Exception("友费控 单据列表查询接口,响应信息【"+resp+"】,异常信息:" + e.getMessage());
        }
        return ret.toString();
    }

    @Override
    public String queryYFKBillAttachList(String apiUrl, Map queryParams, Object bodyParm) throws Exception {
        logger.info("-----查询友费控单据附件列表信息,请求地址:【"+apiUrl+"】,queryParam参数:【"+queryParams.toString()+"】,请求参数Body:【"+bodyParm+"】");
        List<JSONObject> listatts = new ArrayList<JSONObject>();
        String resp = BIPUtil.getPostQueryParamAndBodyParam(apiUrl, queryParams, bodyParm);
        try{
            JSONObject resjson = JSON.parseObject(resp);
            if("200".equals(resjson.getString("code"))){
                JSONArray childrens = resjson.getJSONArray("data").getJSONObject(0).getJSONArray("children");
                if(childrens.isEmpty()) return null;
                for(int i = 0 ; i < childrens.size(); i++){
                    JSONObject item = childrens.getJSONObject(i); //
                    JSONArray fpArrs = item.getJSONArray("children");
                    if(null != fpArrs){// 说明是发票
                        for(int j = 0; j < fpArrs.size(); j++){
                            JSONObject fpObj = fpArrs.getJSONObject(j);
                            JSONObject data = this.HandlerYFKAttach(fpObj);
                            if(data == null || data.isEmpty()) continue;
                            listatts.add(data);
                        }
                    }else{// 说明是附件
                        JSONObject data = this.HandlerYFKAttach(item);
                        if(data == null || data.isEmpty()) continue;
                        listatts.add(data);
                    }
                }

            }else{
                throw new Exception(resp);
            }
        }catch(Exception e){
            String error = "友费控 单据附件列表查询接口,响应信息【"+resp+"】,异常信息:【"+e.getMessage()+"】";
            logger.error(error);
            throw new Exception(error);
        }
        return listatts.toString();
    }

    @Override
    public String queryYFKPrintBillPDF(String apiUrl, Map queryParam, Object bodyParam) throws Exception {
        logger.info("-----查询友费控单据附件列表信息,请求地址:【"+apiUrl+"】,queryParam参数:【"+queryParam.toString()+"】,请求参数Body:【"+bodyParam+"】");
        return BIPUtil.getPostQueryParamAndBodyParam(apiUrl, queryParam, bodyParam);
    }

    /**
     * 处理友费控返回的 单据信息，提取主要信息 即附件地址处理
     * @param json
     * @return
     * @throws Exception
     */
    protected JSONObject HandlerYFKAttach(JSONObject json) throws Exception{
        JSONObject data = new JSONObject();
        String code = json.getString("code");
        int index = code.lastIndexOf(".");
        if(index == -1){
            return  null;
        }
        String attachmentName = code.substring(0, index);
        String attachmentType = code.substring(index+1);
        String attachmentPath = json.getJSONObject("properties").getString("downloadUrlForPreview");
        data.put("attachmentPath", attachmentPath);
        data.put("attachmentType", attachmentType);
        data.put("attachmentName", attachmentName);
        return data;
    }
}
